package com.firexxx.study.lock.monitor.markword;

/**
 * @description: TODO
 * @author: zhuangzhang2
 * @date: 2020-12-18 16:21
 */
public class MarkWordParser {

    /**
     * word : 64bit
     */
    public static IMarkLock parse(long word) {
        IMarkLock markLock = null;

        //取最后2位
        long baseLockFlag = word & 0x03;
        if (baseLockFlag == 0) {
            MarkLightLock markLightLock = new MarkLightLock();
            markLightLock.setLockPoint(word >> 2);

            markLock = markLightLock;
        } else if (baseLockFlag == 2) {
            MarkHeavyLock markWeightLock = new MarkHeavyLock();
            markWeightLock.setMonitorPoint(word >> 2);

            markLock = markWeightLock;
        } else if (baseLockFlag == 3) {
            //gc标记，不处理
        } else {
            baseLockFlag = word & 0x07;
            if (baseLockFlag == 1) {
                MarkNoneLock markNoneLock = new MarkNoneLock();
                markNoneLock.setHashCode((word >> 8) & 0x7FFFFFFF);
                markNoneLock.setAge((word >> 3) & 0x0F);

                markLock = markNoneLock;
            } else {
                MarkBiasedLock markBiasedLock = new MarkBiasedLock();
                markBiasedLock.setThreadId(word >> 10);
                markBiasedLock.setEpoch((word >> 8) & 0x03);
                markBiasedLock.setAge((word >> 3) & 0x0F);

                markLock = markBiasedLock;
            }
        }

        return markLock;
    }

}
